/**
  * 反馈
  */
module game {

    export class FanKuiMediator extends BaseMediator {
        public static NAME: string = "FanKuiMediator";
        private gameType: string = "";
        public constructor(viewComponent: any = null) {
            super(FanKuiMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.HALL.OPEN_FANKUI,
                PanelNotify.HALL.CLOSE_FANKUI
            ];
        }
        private panel: FanKuiPanel;
        private click: boolean = false;//是否点击过输入文本框，防止用户打开界面后直接提交默认文本
        public handleNotification(notification: puremvc.INotification): void {
            this.gameType = notification.getBody();
            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.panel) {
                        this.panel.width = GameConfig.curWidth()
                        this.panel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.HALL.OPEN_FANKUI: {
                    this.panel = null;
                    this.panel = new FanKuiPanel();
                    this.showUI(this.panel, 2, 1, true);
                    break;
                }
                case PanelNotify.HALL.CLOSE_FANKUI: {
                    this.closePanel(0);
                    break;
                }
            }
        }

        /**
         * 初始化面板ui
         */
        public initUI(): void {
            this.panel.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeButtonClick, this);
            this.panel.confirm.addEventListener(egret.TouchEvent.TOUCH_TAP, this.confirmBrokeButtonClick, this);
            this.panel.text.addEventListener(egret.FocusEvent.FOCUS_IN, this.textClick, this);
        }

        /**
         * 初始化面板数据
         */
        public initData(): void {
            this.click = false;
        }
        private textClick(event: egret.TouchEvent) {
            this.panel.text.removeEventListener(egret.FocusEvent.FOCUS_IN, this.textClick, this);
            this.panel.text.text = ""
            this.panel.text.textColor = 0xdfce94

            this.click = true;
        }
        private closeButtonClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            this.closePanel(1);
        }

        private confirmBrokeButtonClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            var text: string = this.panel.text.text;
            if (this.click == false || text == "") {
                EffectUtils.textFadeAway("提交内容必须为10-60个字符")//弹出提示消息
                return;
            }
            // 去除所有空格:
            // str = str.replace(/\s+/g, "");
            // 去除两头空格:
            // str = str.replace(/^\s+|\s+$/g, "");
            var text = text.replace(/^\s+|\s+$/g, "");
            if (text.length < 10 || text.length > 60) {
                EffectUtils.textFadeAway("提交内容必须为10-60个字符")//弹出提示消息
                return;
            }
            var url =InstructionsEnum.IP_API + "/GameWebApplication/SubmitMessage.ashx"
            var httpRequest = new XMLHttpRequest();
            httpRequest.onreadystatechange = function () {
                if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                    EffectUtils.textFadeAway("感谢您的反馈！我们会继续改进！")//弹出提示消息
                }
            };
            httpRequest.open('POST', url, true);
            httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            // httpRequest.setRequestHeader("Access-Control-Allow-Credentials", "true");
            httpRequest.send(JSON.stringify({
                "MsgType": 0,
                "MsgTiltle": "反馈意见",
                "MsgContent": text,
                "UserId": GameVO.getVo().dwUserID
            }));
            this.closePanel(1);
        }
    }
}
